package com.vhall.component.service.filter.impl;

import com.alibaba.fastjson.JSON;
import com.vhall.component.entity.export.ExportEntity;
import com.vhall.component.entity.filter.FilterWordsEntity;
import com.vhall.component.entity.filter.vo.*;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.export.ExportService;
import com.vhall.component.service.filter.FilterWordsApiService;
import com.vhall.component.service.filter.FilterWordsConsoleService;
import com.vhall.component.service.filter.FilterWordsService;
import com.vhall.component.service.room.RoomInnerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author longzhen.gong
 * @date 2021/7/5 11:20
 */
@Service
public class FilterWordsConsoleServiceImpl implements FilterWordsConsoleService {

    @Autowired
    private FilterWordsService filterWordsService;

    @Autowired
    private FilterWordsApiService filterWordsApiService;

    @Autowired
    private RoomInnerService roomInnerService;

    @Autowired
    private ExportService exportService;

    @Override
    public PageVO<FilterWordsListRspVO> list(FilterWordsListReqVO reqVO, Integer accountId) {
        return filterWordsService.list(reqVO, accountId);
    }

    @Override
    public Integer delete(String ids) {
        return filterWordsService.delete(ids);
    }

    @Override
    public FilterWordsCreateRspVO create(String roomId, String keyword, Integer accountId, Integer userId) {
        keyword = StringUtils.trim(keyword);
        FilterWordsEntity filterWordsEntity = filterWordsService.create(keyword, accountId, userId, roomId);
        FilterWordsCreateRspVO result = new FilterWordsCreateRspVO();
        BeanUtils.copyProperties(filterWordsEntity, result);
        return result;
    }

    @Override
    public Boolean update(Integer id, String keyword, String roomId, Integer accountId) {
        return filterWordsService.update(id, keyword, roomId, accountId, accountId);
    }

    @Override
    public FilterWordsImportRspVO importFile(String roomId, MultipartFile file, Integer accountId) {
        return filterWordsService.importFile(roomId, file, accountId, accountId);
    }

    @Override
    public Boolean export(String roomId, Integer accountId, String fileName, String beginTime, String endTime) {
        accountId = accountId == null ? 0 : accountId;
        RoomsEntityDTO roomsEntityDTO = roomInnerService.getRoomInfo(roomId);
        if (roomsEntityDTO == null) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        Map<String, Object> param = new HashMap<>(8);
        List<String> header = Arrays.asList("直播ID", "直播名称", "昵称", "用户id", "时间", "消息内容", "分类");
        param.put("account_id", accountId);
        param.put("il_id", roomsEntityDTO.getIlId());
        param.put("file_name", fileName);
        param.put("start_time", beginTime);
        param.put("end_time", endTime);
        ExportEntity ee = new ExportEntity();
        ee.setSourceId(roomsEntityDTO.getRoomId());
        ee.setTitle(JSON.toJSONString(header));
        ee.setParams(JSON.toJSONString(param));
        ee.setExt("xlsx");
        ee.setExport("filterword");
        ee.setIlId(roomsEntityDTO.getIlId());
        ee.setAccountId(accountId);
        ee.setFileName(fileName);
        ee.setCallback("filterWordsService:export");
        ee.setRoomId(roomId);
        return exportService.createExport(ee);
    }

    @Override
    public String getFilterWords(FilterWordsGetReqVO reqVO) {
        return filterWordsApiService.getFilterWords(reqVO);
    }

    @Override
    public Boolean reportFilterWords(FilterWordsReportReqVO reqVO, Integer accountId) {
        return filterWordsApiService.reportFilterWords(reqVO, accountId);
    }

}
